package com.tsd.system.service.impl;

import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.dao.SysRoleDataAccessRuleMapper;
import com.tsd.system.entity.SysRoleDataAccessRule;
import com.tsd.system.entity.SysRoleDataAccessRuleExt;
import com.tsd.system.entity.SysUser;
import com.tsd.system.service.SysRoleDataAccessRuleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysRoleDataAccessRule 服务实现层
 *
 * @author david
 * @date 2021/07/08 15:49:18
 */
@Service
public class SysRoleDataAccessRuleServiceImpl extends BaseServiceImpl implements SysRoleDataAccessRuleService {

    @Resource
    private SysRoleDataAccessRuleMapper sysRoleDataAccessRuleMapper;

    @Override
    public SysRoleDataAccessRule getSysRoleDataAccessRuleById(Long id) throws Exception {
        return sysRoleDataAccessRuleMapper.selectByPrimaryKey(id);
    }

    @Override
    public SysRoleDataAccessRuleExt queryBySid(String sid) throws Exception {
        return sysRoleDataAccessRuleMapper.selectBySid(sid);
    }

    @Override
    public void deleteSysRoleDataAccessRule(Long id, SysUser opUser) throws Exception {
        SysRoleDataAccessRule old = sysRoleDataAccessRuleMapper.selectByPrimaryKey(id);
        super.checkEmpty(old, "该记录不存在");
        super.autoInjectBaseData(old, opUser, TYPE_DELETE);
        sysRoleDataAccessRuleMapper.updateByPrimaryKeySelective(old);
    }

    @Override
    public void saveSysRoleDataAccessRule(SysRoleDataAccessRule record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            super.autoInjectBaseData(record, opUser, TYPE_CREATE);
            sysRoleDataAccessRuleMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, TYPE_UPDATE);
            sysRoleDataAccessRuleMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public void saveSysRoleDataAccessRuleList(List<SysRoleDataAccessRule> list, SysUser opUser) throws Exception {
        if (!HlpUtils.isEmptyList(list)) {
            sysRoleDataAccessRuleMapper.deleteList(list.get(0).getRole_sid());
            for (SysRoleDataAccessRule obj : list) {
                super.autoInjectBaseData(obj, opUser, TYPE_CREATE);
            }
            sysRoleDataAccessRuleMapper.insertList(list);
        }
    }

    @Override
    public List<SysRoleDataAccessRule> queryValidByRoleList(List<String> roleList) throws Exception {
        return sysRoleDataAccessRuleMapper.selectValidByRoleList(roleList);
    }

    @Override
    public List<SysRoleDataAccessRuleExt> findSysRoleDataAccessRules(Map<String, Object> params, PageBean page) throws Exception {
        return sysRoleDataAccessRuleMapper.selectByPage(params, page);
    }

    @Override
    public List<SysRoleDataAccessRuleExt> findSysRoleDataAccessRules(Map<String, Object> params) throws Exception {
        return sysRoleDataAccessRuleMapper.selectByParams(params);
    }
}
